package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserGiftDispose;
import com.lantosec.hmrp.modules.archives.mapper.UserGiftDisposeMapper;
import com.lantosec.hmrp.modules.archives.service.IUserGiftDisposeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserGiftDisposeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * <p>
 * 领导干部拒收或上交礼金、礼品情况登记表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-12
 */
@Service
@Primary
@Log4j2
public class UserGiftDisposeServiceImpl extends ServiceImpl<UserGiftDisposeMapper, UserGiftDispose> implements IUserGiftDisposeService {

    @Override
    public void saveUserGift(UserGiftDisposeVO vo) {
        UserGiftDispose userGiftDispose = vo.convertToEntity();
        userGiftDispose.setUserGiftDisposeId(UUIDUtils.get());
        //获取当前用户id和名称
        userGiftDispose.setCreateId(TokenUtil.getUserId());
        userGiftDispose.setCreateUser(TokenUtil.getName());
        userGiftDispose.setCreateTime(DateUtils.nowDate());
        userGiftDispose.setVerifyStatus(1);
        this.save(userGiftDispose);
    }

    @Override
    public void updateUserGift(UserGiftDisposeVO vo) {
        String userGiftDisposeId = vo.getUserGiftDisposeId();
        //新建一条数据
        //saveUserGift(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userGiftDisposeId)){
            UserGiftDispose userGiftDispose = vo.convertToEntity();
            userGiftDispose.setVerifyStatus(1);
            this.updateById(userGiftDispose);
        }else {
            saveUserGift(vo);
        }
    }

    @Override
    public List<UserGiftDispose> getUserGiftDispose(String userId) {
        List<UserGiftDispose> userGiftDisposeList = this.list(new QueryWrapper<UserGiftDispose>().eq(UserGiftDispose.ColName.user_id.toString(),userId).orderByDesc(UserGiftDispose.ColName.create_time.toString()));
        return userGiftDisposeList;
    }

    @Override
    public List<UserGiftDispose> getUserGiftDisposeVerify(String userId) {
        List<UserGiftDispose> userGiftDisposeList = this.list(new QueryWrapper<UserGiftDispose>().eq(UserGiftDispose.ColName.user_id.toString(),userId).in(UserGiftDispose.ColName.verify_status.toString(),0,2));
        return userGiftDisposeList;
    }

    @Override
    public void verifyUserGiftDispose(UserGiftDisposeVO vo) {
        UserGiftDispose userGiftDispose = vo.convertToEntity();
        Integer verifyStatus = userGiftDispose.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userGiftDispose.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userGiftDispose.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userGiftDispose.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userGiftDispose.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userGiftDispose.setVerifyId(TokenUtil.getUserId());
        userGiftDispose.setVerifyUser(TokenUtil.getName());
        userGiftDispose.setVerifyTime(DateUtils.nowDate());
        this.updateById(userGiftDispose);
    }

    @Override
    public List<UserGiftDispose> getUserGiftDisposeHistory(String userId) {
        List<UserGiftDispose> userGiftDisposeList = this.list(new QueryWrapper<UserGiftDispose>().eq(UserGiftDispose.ColName.user_id.toString(),userId).eq(UserGiftDispose.ColName.verify_status.toString(),4));
        return userGiftDisposeList;
    }
}
